﻿using System;

namespace MyDemo.Exceptions
{
    /// <summary>
    /// 该Class用来测试异常抛出时相关上下文栈的调用情况
    /// http://www.cnblogs.com/JerryTian/archive/2012/09/24/2699459.html
    /// </summary>
    public class ExceptionClass
    {
        /// <summary>
        /// 入口方法
        /// </summary>
        public static void Test()
        {
            ExceptionClass ec = new ExceptionClass();

            try
            {
                ec.ExceptionThrow1();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.WriteLine("---------------------------------------------------------------------");

            try
            {
                ec.ExceptionThrow2();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.WriteLine("---------------------------------------------------------------------");

            try
            {
                ec.ExceptionThrow3();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.WriteLine("---------------------------------------------------------------------");

            try
            {
                ec.ExceptionThrow4();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.WriteLine("---------------------------------------------------------------------");

            Console.ReadKey();
        }

        /// <summary>
        /// 抛出异常方法
        /// </summary>
        public void ExceptionThrow1()
        {
            try
            {
                // 调用原始异常抛出方法来抛出异常
                this.ExceptionMethod();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 抛出异常方法1
        /// </summary>
        public void ExceptionThrow2()
        {
            try
            {
                this.ExceptionMethod();
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        /// <summary>
        /// 抛出异常方法2
        /// </summary>
        public void ExceptionThrow3()
        {
            try
            {
                this.ExceptionMethod();
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 抛出异常方法3
        /// </summary>
        public void ExceptionThrow4()
        {
            try
            {
                this.ExceptionMethod();
            }
            catch (Exception ex)
            {
                throw new Exception("经过进一步包装的异常", ex);
            }
        }

        /// <summary>
        /// 原始异常抛出方法
        /// </summary>
        private void ExceptionMethod()
        {
            throw new DivideByZeroException();
        }
    }
}